/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.openjawtech.xscraper.scraper.util;

import java.io.IOException;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import com.openjawtech.xscraper.scraper.exception.ConfigurationException;

/**
 *
 * @Created on 13-Apr-2009, 02:54:27
 * @author Wenbo.Sun
 */
public class PropertiesUtil {


    public static Properties getProperties(String file) throws ConfigurationException {
        Properties properties = null;
        try {
            properties = new Properties();
            FileInputStream fis = new FileInputStream(file);
            properties.load(fis);
            fis.close();
        } catch (IOException ex) {
            throw new ConfigurationException("get properties failed!", ex);
        }
        return properties;
    }

    public static void savePropertiesToFile(Properties properties, String file) throws ConfigurationException {
        try {
            FileOutputStream fos = new FileOutputStream(file);
            properties.store(fos, "properties for client");
            fos.close();
        } catch (IOException ex) {
            throw new ConfigurationException("save properties to file failed!", ex);
        }
    }

    public static void updateProperties(String file, String name, String value) throws ConfigurationException {
        Properties properties = getProperties(file);
        properties.setProperty(name, value);
        savePropertiesToFile(properties, file);
    }
}
